<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/9
 * Time: 下午6:04
 */

namespace app\api\service;


use app\api\controller\v1\UserAuth as UserAuthController;
use app\api\model\User as UserModel;
use think\Session;

class UserService extends Base
{
    public function addInfo($guid,$data)
    {
        $where = [
            'login_name' => $data['login_name']
        ];
        $userInfo = $this->getInfo($where,['login_name']);
        //添加检查
        if (!is_null($userInfo))
            set_error_exception('login_name_exists');
        //检查结束
        $row = UserModel::addInfo($guid,$data)->id;
        $result = $this->dbOperationResult($row);
        return $result;
    }

    public function updateInfo($guid,$data,$where)
    {
        //数据不为空检查
        $field = $this->checkParamIsNull($data);

        $odata = $this->getInfo($where,$field);
        //数据不存在检查
        if (empty($odata)) {
            set_error_exception('data_not_exists');
        }
        //数据无修改检查
        foreach ($data as $k => $v) {
            if (($k === 'password' && password_verify($v,$odata[$k])) || $v === $odata[$k]) {
                set_error_exception('error',['code'=>10001,'msg'=>$k.'无修改']);
            }
        }
        //数据唯一检查
        $unqiue = ['login_name'];
        foreach ($unqiue as $k => $v) {
            if (isset($data[$v])) {
                $map = [
                    $v => $data[$v]
                ];
                if(!empty($this->getInfo($map,['id'])))
                    set_error_exception('error',['code'=>10001,'msg'=>$v.'重复']);
            }
        }

        $row = UserModel::updateInfo($guid,$odata,$data,$where);
        $result = $this->dbOperationResult($row);
        return $result;
    }

    public function getUserInfo($where)
    {
        $data = $this->getInfo($where,['login_name','name','gender','birthday','id_card_number','tel']);
        //数据不存在检查
        if (empty($data)) {
            set_error_exception('data_not_exists');
        }
        return $data;
    }

    public function getUserInfoListByMap($where,$page)
    {
        $result = $list = [];
        if (!empty($page)) {
            $count = UserModel::where($where)->count();
            $list_max_rows = config('list_rows_20');
            $total_page = ceil($count/$list_max_rows);
            $current_page = (int)$page;
            if ($count > 0) {
                $obj = UserModel::getInfoList($where,['guid','login_name','name','org_id','enabled','gender','birthday','id_card_number','tel'],$page,'org');
                foreach ($obj as $k => $v) {
                    $list[] = $v->toArray();
                }
            }
            $result = [
                'count' => $count,
                'list_max_rows' => $list_max_rows,
                'list_rows' => count($list),
                'total_page' => $total_page,
                'current_page' => $current_page,
                'list' => $list
            ];
        } else {
            $obj = UserModel::getInfoList($where,['guid','login_name','name','org_id','enabled','gender','birthday','id_card_number','tel'],null,'org');
            foreach ($obj as $k => $v) {
                $result[] = $v->toArray();
            }
        }
        return $result;
    }

    public function login($data)
    {
        $account = $data['login_name'];
        $password = $data['password'];
        $where['login_name'] = $account;
        $odata = $this->getInfo($where);
        //数据不存在检查
        if (empty($odata)) {
            set_error_exception('data_not_exists');
        }
        //密码验证
        if (!password_verify($password,$odata['password'])) {
            set_error_exception('password_error');
        }
        //是否可用
        if ($odata['enabled'] !== '正常') {
            set_error_exception('account_disenabled');
        }
        $row = (new UserAuthController)->setLoginGlobal($odata);
        if ($row === false) {
            set_error_exception('login_error');
        }
        $token = Session::get('token');
        return ['token'=>$token];
    }

    protected function getInfo($where,$field=true)
    {
        $infoObj = UserModel::getInfo($where,$field);
        is_null($infoObj) ? : $infoObj = $infoObj->toArray();
        return $infoObj;
    }
}